import {
	getPhoneApi,
	getTokenApi
} from '../common/api/user.js'
import store from '../store/store'
export const myGridFilter = (str) => {
	const convert = {
		answered: '我的答题',
		medication: '我的用药',
		server: '我的服务'
	}
	for (let key in convert) {
		if (convert[key] === str) {
			return key
		}
	}
}
let valve = false
//登录方法
export const userLogin = async () => {
	if (!valve) {
		uni.$showToast('自动登录中, 请稍候...')
		valve = true
		const r = await wx.login()
		//获取手机号
		const res = await getPhoneApi(r.code)
		console.log(res, '手机号,openid')
		// return res.data
		if (res.data?.phone) {
			//有手机号
			const {
				data: {
					accessToken: token
				}
			} = await getTokenApi(res.data.openId, res.data.phone)
			console.log(token, 'token')
			store.commit('user/MusetToken', token)
			uni.reLaunch({
				url: "/pages/home/home"
			})
			valve = false
			return token
		}
		//没有手机号
		uni.navigateTo({
			url: "/subpkg/login/login"
		})
		valve = false
		return false
	}
}

//过滤医院和科室
export const filterDepartmentHospital = (arr) => {
	const newArr = [{
		label: '全部',
		value: '',
		extra: 0
	}]
	arr.forEach((item, index) => {
		if (typeof item === "string") {
			newArr.push({
				label: item,
				value: item,
				extra: index + 1
			})
		} else {
			newArr.push({
				label: item.label,
				value: item.label,
				extra: index + 1
			})
		}
	})
	return newArr
}
